package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptService deptService;
    @Resource
    private EmpExprService empExprService;

    @Override
    public PageResult<Emp> page1(EmpQueryParam param) {
        Page<Emp> objectPage = Page.of(param.getPage(), param.getPageSize());
        objectPage.addOrder(OrderItem.desc("update_time"));
        Page<Emp> page = lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(), Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd())
                .page(objectPage);
//        return new PageResult<>(objectPage.getTotal(), objectPage.getRecords());
        List<Emp> emps = page.getRecords();
        List<Integer> list = emps.stream().map(Emp::getDeptId).toList();
        List<Dept> depts = deptService.listByIds(list);
        emps.forEach(emp -> {
            depts.forEach(dept -> {
                if (emp.getDeptId().equals(dept.getId())) {
                    emp.setDeptName(dept.getName());
                }
            });
        });
        return new PageResult<>(objectPage.getTotal(), emps);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save1(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
        });
        empExprService.saveBatch(exprList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Emp getById1(Integer id) {
        Emp e = getById(id);
        e.setExprList(
                empExprService.list(
                        new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id)
                )
        );
        return e;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update1(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
        });
        empExprService.saveBatch(exprList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeBatchByIds1(List<Integer> ids) {
        removeBatchByIds(ids);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }
}
